/*
 * $RCSfile: Processor_44_Enter.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-16  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.i366.processor;

import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.util.Map;


/**
 * 处理请求进入房间 / 包括快速开始游戏
 * <p>Title: EnterProcessor</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class Processor_44_Enter extends Handler {
	private final static long LL = System.currentTimeMillis();
	private Logger logger = Logger.getLogger(Processor_44_Enter.class);
	public byte[] handleRequest() {
		if (System.currentTimeMillis() - LL < 60 * 2000) {
			return pusUser(1);
		}
		long l = System.currentTimeMillis();
		logger.debug("EnterProcessor userId is : " + this.getGameRequest().getUserId());
		Request request = getGameRequest();
		int[][] int2 = {{131,I366ClientPickUtil.TYPE_INT_4},
				{132,I366ClientPickUtil.TYPE_INT_4},
				{60,I366ClientPickUtil.TYPE_INT_1},
				{61,I366ClientPickUtil.TYPE_INT_1}
		};
		Map<Integer, Object> map = I366ClientPickUtil.pickAll(request.getBt(), int2);
		int roomPath = (Integer)map.get(131);
		int roomId = (Integer)map.get(132);
		int action = map.get(60) == null ? -1 : (Integer)map.get(60);
		int reconnect = map.get(61) == null ? 1 : (Integer)map.get(61);
		Room room = null;
		try {
			room = Cache.getRoom(roomId, roomPath);
		} catch (Exception e) {
			logger.error("not found room/roomPath :" + roomId + "/" + roomPath , e);
			return null;
		}
		int size = room == null ? 0 :room.getAudMap().size();
		if (room != null && size >= 16 && reconnect == 1) {
			logger.debug("房间人数过多...roomId" + room.getRoomId());
			return pusUser(1);
		}
		UserInfo userInfo = Cache.getOnlineUserInfo().get(request.getUserId());
		//
		int status = 2;
		//掉线重连
		if (reconnect == 0) {
			if (userInfo != null && room != null) {
				//掉线重连
				status = room.getRoomService().reconnectRoom(userInfo, (GameClient) request.getClient(), action, roomId,roomPath);
			}
		}else {
			if (userInfo != null) {
				Room room2 = Cache.getRoom(userInfo.getRoomId(),userInfo.getRoomPath());
				if (room2 != null) {
					// 玩家离开房间
					status = room2.getRoomService().ercijinru(request.getUserId(),(GameClient) request.getClient(), action, roomId,roomPath);
				}
			}

			if (room != null) {
				if (userInfo != null && request.getUserId() != userInfo.getUserId() ) {
					//玩家离开房间
					status = room.getRoomService().ercijinru(request.getUserId(), (GameClient)request.getClient(), action , roomId ,roomPath);
				}else if (userInfo == null){
					//玩家离开房间
					status = room.getRoomService().ercijinru(request.getUserId(), (GameClient)request.getClient(), action , roomId ,roomPath);
				}
			}
		}

		//-1 不做任何操作 1失败 2逻辑往下走
		if (status == -1) {
			return null;
		}else if (status == 1) {
			return pusUser(1);
		}

		UserInfoDao userInfoDao = new UserInfoDaoImp();
		try {
			userInfo = userInfoDao.getUserInfo(request.getUserId());
		} catch (SQLException e) {
			e.printStackTrace();
			return pusUser(1);
		}
		if (userInfo == null) {
			logger.debug("没有找到该用户！");
			return pusUser(1);
		}
		LeveRoom leaveRoom = Cache.getLeveRoom(roomId, roomPath);
		//筹码不在区间 则不给予进入  如果是掉线重新则让进入
		if (reconnect != 0) {
			if (leaveRoom.getRoomType() == 1 && (leaveRoom.getMin() > userInfo.getChip() || leaveRoom.getMax() < userInfo.getChip())) {
				logger.debug("筹码不在区间范围不能进入房间！");
				return pusUser(1);
			}
		}
		//掉线重连并且筹码不够 则当做游客
		if (reconnect == 0 && leaveRoom.getMin() > userInfo.getChip()) {
			action = 1;
		}
		//初始化
		userInfo.setOnlinetTime(System.currentTimeMillis());
		userInfo.setCline((GameClient)request.getClient());
		userInfo.setRoomId(roomId);
		userInfo.setRoomPath(roomPath);

		RobotManage robotManage = null;
		if (room == null) {
			LeveRoom leveRoom = Cache.getLeveRoom(roomId, roomPath);
			if (com.ai.dz.config.Constant.kaiguan) {
				robotManage = new RobotManage();
				room = robotManage.initRobotManage(leveRoom, roomId, 1);
			}else {
				room = leveRoom.initRoom(roomId);
			}
		}
		byte bytes[];
		if (action == 0) {

			bytes =  room.getRoomService().fastGame(userInfo.getUserId(), userInfo);
		}else {
			//进入房间完成
			//
//			byte[] bytes = room.getRoomService().getRoomDate(Constant.REQ_GAME_ENTER_ROOM , -1);
			bytes = room.getRoomService().accessRoom(userInfo);
		}
		if (robotManage != null) {
			robotManage.initOk();
		}
		logger.debug(".....进入房间用去的时间: " + (System.currentTimeMillis() - l));
		return bytes;

	}

	private byte[] pusUser(int status) {
		Object[][] objs2 = {
				{60,status,I366ClientPickUtil.TYPE_INT_1}
		};
		byte[] bytes = I366PickUtil.packAll(objs2, Constant.REQ_GAME_ENTER_ROOM);
		return bytes;
	}

	/**
	 * 如果别的房间还在玩 
	 * T掉上一个人
	 * @param userId
	 * @return
	 */
	private void t(int userId) {
		UserInfo userInfo = Cache.getOnlineUserInfo().get(userId);
		if (userInfo == null) {
			return;
		}
		Room room = Cache.getRoom(userInfo.getRoomId(), userInfo.getRoomPath());
		if (room != null && room.getRoomService() != null) {
			room.getRoomService().userTimeOut(userInfo);
		}
	}
	public Object makeObject() throws Exception {
		return new Processor_44_Enter();
	}

}

